package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.constant.ProductConstant;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private AttrAttrgroupRelationDao relationDao;

    @Resource
    private AttrGroupDao attrGroupDao;

    /**
     * @param params:    分页请求参数
     * @param attrType:  属性类型 1 基本属性 0 销售属性
     * @param catelogId: 三级分类ID
     * @Description: 获取分类基本属性
     * @Author: LiQianJin
     * @Date: 2024/3/30 22:20
     * @return: java.util.List<com.atguigu.gulimall.product.vo.AttrRespVO>
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public PageUtils queryBaseAttrListPage(Map<String, Object> params, String attrType, Long catelogId) {
        // 查询基本属性 销售属性
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type",
                "base".equalsIgnoreCase(attrType) ? ProductConstant.ProductAttrTypeEnum.BASE_TYPE_ENUM.getCode() : ProductConstant.ProductAttrTypeEnum.SALE_TYPE_ENUM.getCode());
        // 组装检索条件
        String retrieval = (String) params.get("key");
        if (StringUtils.isNotEmpty(retrieval)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("attr_id", retrieval).or().like("attr_name", retrieval);
            });
        }
        if (catelogId != 0) {
            queryWrapper.eq("catelog_id", catelogId);
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
        PageUtils pageUtils = new PageUtils(page);
        List<AttrRespVO> respVos = page.getRecords().stream().map((attrEntity) -> {
            AttrRespVO attrRespVo = new AttrRespVO();
            BeanUtils.copyProperties(attrEntity, attrRespVo);
            // 获取基本属性---所属分组---销售属性不具备分组
            if ("base".equalsIgnoreCase(attrType)) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
                    if (null != attrGroupEntity) {
                        attrRespVo.setAttrGroupId(attrGroupEntity.getAttrGroupId());
                        attrRespVo.setGroupName(Optional.ofNullable(attrGroupEntity).map(AttrGroupEntity::getAttrGroupName).orElse(null));
                    }
                }
            }
            // 获取所属分类
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(Optional.ofNullable(categoryEntity).map(CategoryEntity::getName).orElse(null));
            }
            return attrRespVo;
        }).collect(Collectors.toList());
        // 返回组装的所有规格参数信息
        pageUtils.setList(respVos);
        return pageUtils;
    }
}